package com.ch.mobilesha.base;

import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import com.ch.mobilesha.common.CommonData;
import com.ch.mobilesha.common.CommonResult;
import com.ch.mobilesha.tools.ViewTools;

public abstract class BaseRequestAsyncTask extends AsyncTask<Object, Object, Object> {

	protected Context context;

	protected RequestServerListener requestServerListener;

	public BaseRequestAsyncTask(){
		
	}
	public BaseRequestAsyncTask(Context context, RequestServerListener requestServerListener) {
		this.requestServerListener = requestServerListener;
		this.context = context;
	}
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (requestServerListener != null) {
			requestServerListener.onRequestServerBegin();
		}
	}

	@Override
	protected void onPostExecute(Object result){
		super.onPostExecute(result);
		try{
			if (requestServerListener != null) {
				requestServerListener.onRequestServerEnd(result);
			}else{
				if(result!=null && ((JSONObject)result).get("result").equals(CommonResult.SUCCESS)){
				}else if(result!=null && ((JSONObject)result).getString("result").equals(CommonResult.MESSAGE)){
					if(context == CommonData.currentActivity){
						ViewTools.showMessage(CommonData.currentActivity,  ((JSONObject)result).getString("message"));
					}
				}else{
					if(context == CommonData.currentActivity){
						ViewTools.showMessage(CommonData.currentActivity, "网络错误");
					}
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public interface RequestServerListener {
		public void onRequestServerBegin();

		public void onRequestServerEnd(Object result);
	}
}
